<template>
  <div
    class="standardLibrary"
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
  >
    <lb-layout>
      <aps-form
        :config="apsForm.formConfig"
        :model="apsForm.formData"
        :formRef="apsForm.formRef"
        :labelWidth="apsForm.labelWidth"
        ref="form"
      ></aps-form>
      <lb-table
        :column="tableData.column"
        :data="tableData.data"
        pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 40, 60, 80]"
        :pager-count="5"
        :current-page.sync="currentPage"
        :total="total"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @p-current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        class="addCourseTypeTable"
      >
      </lb-table>
    </lb-layout>
  </div>
</template>

<script>
import DictionariesSelect from "@/components/DictionariesSelect/index.vue";
import {
  versionList,
  rationCriterionList,
} from "@/api/library/standardLibrary";
import { formatDate } from "@/utils/index";

export default {
  name: "standardLibrary",
  dicts: ["energy_type", "target_type", "energy_ration_criterion_status"],
  components: {
    DictionariesSelect,
  },
  data() {
    return {
      versionNumberlist: [],
      apsForm: {
        formRef: "form",
        labelWidth: "120px",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            version: {
              typeName: "el-input",
              placeholder: "定额标准版本号",
            },
            energyType: {
              typeName: "el-select",
              render: (h) => {
                let that = this;
                return (
                  <div>
                    {
                      <DictionariesSelect
                        showType={0}
                        valueType={1}
                        datas={that.$refs?.form?.getFieldsValue().energyType}
                        selectArr={that.dict.type.energy_type}
                        onHandleSelect={(val) => {
                          that.$refs?.form?.setFieldValue("energyType", val);
                          that.$forceUpdate();
                        }}
                        placeholder="能耗类型"
                      ></DictionariesSelect>
                    }
                  </div>
                );
              },
            },
            targetType: {
              typeName: "el-select",
              render: (h) => {
                let that = this;
                return (
                  <div>
                    {
                      <DictionariesSelect
                        showType={0}
                        valueType={1}
                        datas={that.$refs?.form?.getFieldsValue().targetType}
                        selectArr={that.dict.type.target_type}
                        onHandleSelect={(val) => {
                          that.$refs?.form?.setFieldValue("targetType", val);
                          that.$forceUpdate();
                        }}
                        placeholder="指标类型"
                      ></DictionariesSelect>
                    }
                  </div>
                );
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      tableData: {
        column: [
          // {
          //   type: "selection",
          // },
          {
            prop: "version",
            label: "定额标准版本号",
            showOverflowTooltip: true,
          },
          {
            prop: "energyType",
            width: "200",
            label: "能耗类型",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_type}
                    value={scope.row.energyType.toString()}
                  />
                </div>
              );
            },
            // render: (h, params) => {
            //   let testType =
            //     params.row.energyType == null
            //       ? ""
            //       : params.row.energyType.toString();
            //   let typeArr = this.dict.type.energy_type;
            //   let typeName = "";
            //   typeArr.forEach((element) => {
            //     if (element.value == testType) {
            //       typeName = element.label;
            //     }
            //   });
            //   return h("div", {}, typeName);
            // },
          },
          {
            prop: "targetType",
            // width: "200",
            label: "指标类型",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.target_type}
                    value={scope.row.targetType.toString()}
                  />
                </div>
              );
            },
            // render: (h, params) => {
            //   let testType =
            //     params.row.targetType == null
            //       ? ""
            //       : params.row.targetType.toString();
            //   let typeArr = this.dict.type.target_type;
            //   let typeName = "";
            //   typeArr.forEach((element) => {
            //     if (element.value === testType) {
            //       typeName = element.label;
            //     }
            //   });
            //   return h("div", {}, typeName);
            // },
          },
          {
            prop: "status",
            width: "200",
            label: "状态",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_ration_criterion_status}
                    value={scope.row.status.toString()}
                  />
                </div>
              );
            },
            // render: (h, params) => {
            //   let testType =
            //     params.row.status == null ? "" : params.row.status.toString();
            //   let typeArr = this.dict.type.energy_ration_criterion_status;
            //   let typeName = "";
            //   typeArr.forEach((element) => {
            //     if (element.value === testType) {
            //       typeName = element.label;
            //     }
            //   });
            //   return h("div", {}, typeName);
            // },
          },
          {
            prop: "makingTime",
            // width: "",
            label: "制定时间",
            render: (h, params) => {
              let that = this;
              return (
                <div>
                  <span>{that.filterTime(params.row.makingTime)}</span>
                </div>
              );
            },
          },
          {
            prop: "makingUserName",
            width: "200",
            label: "制定人员",
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      multipleSelection: [],
    };
  },
  created() {},
  mounted() {
    this.initData();
    // this.getversionNumberListData();
  },
  methods: {
    filterTime(val) {
      return formatDate(val);
    },
    // 版本号
    getversionNumberListData() {
      versionList({})
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          if (res.data && res.data.length > 0) {
            res.data.map((item) => {
              let obj = {
                label: item,
                value: item,
              };
              this.versionNumberlist.push(obj);
            });
          } else {
            this.versionNumberlist = [];
          }
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!20060");
        });
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {};
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      if (data) {
        query.version = data?.version ?? null;
        query.energyType = data?.energyType ?? null;
        query.targetType = data?.targetType ?? null;
      }
      this.$store.commit("app/updateApiCount", "add");
      await rationCriterionList(query)
        .then((res) => {
          // console.log(res, "列表");
          this.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          this.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!20061");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let data = {};
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      data.version = this.$refs?.form?.getFieldsValue().version;
      data.energyType = this.$refs?.form?.getFieldsValue().energyType;
      data.targetType = this.$refs?.form?.getFieldsValue().targetType;
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleReset() {
      //重置
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>

<style scoped lang="scss">
.standardLibrary {
  width: 100%;
}
</style>

